<?php

namespace app\common\model\merchant;
use app\common\model\BaseModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 商户统计表
 */
class MerchantStatisticModel  extends BaseModel
{

    /**
     * 业绩统计分组分页
     * @param $table |表
     * @param $time_where | 时间条件
     * @param $page
     * @param $page_size
     * @return array|mixed
     * @throws DbException
     */
    public function getMerchantStatisticData($table , $time_where , $page , $page_size)
    {
        $merchant = $this->db($table)
            ->where($time_where ?? [])
            ->field('mer_id,recharge_money,account_money,
            manager_red_packet,manager_point,code_number,manager_num,
            type,status,statistics_date')
            ->paginate(['page' => $page ?: 1, 'list_rows' => $page_size ?: 15]);
        if (!empty($merchant['data'])){
            $status_array = ['1' => '正常' , '2' => '流失客户|禁用客户'];
            $type_array = ['1' => '正常' , '2' => '沉睡'];
            $merchant_info = $this->db('merchant_info')->column('concat(title,\'|\',mobile,\'|\',mer_id) as title','mer_id');
            foreach ($merchant['data'] as  &$v) {
                $v['title'] = $merchant_info[$v['mer_id']]['title'] ?? '';
                $v['status_name'] = $status_array[$v['status']] ?? '未知';
                $v['type_name'] = $type_array[$v['type']] ?? '未知';
            }
        }
        return $merchant;
    }


    /**
     * 业绩查询
     * @param $table |表
     * @param $mer_id
     * @return array|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getStatisticData($table , $mer_id, $filed, $where = [])
    {
        $merchant = $this->db($table)
            ->where('mer_id' , $mer_id)->where($where ?? [])
            ->group("statistics_date")
            ->field($filed)
            ->order('statistics_date desc')
            ->select();
        return $merchant;
    }

    /**
     * 业绩查询添加
     * @param $table |表
     * @param $data
     * @throws DbException
     */
    public function add($table , $data)
    {
        if ((string)$table === 'merchant_statistic_month'){
            $date = date('Y-m');
            $string = 'max(to_char(create_at,\'YYYY-MM\')) as create_at';
            $where = ['statistics_date' => $date];
        }

        if ((string)$table === 'merchant_statistic_day'){
            $date = date('Y-m-d');
            $string = 'max(to_char(create_at,\'YYYY-MM-DD\')) as create_at';
            $datetime = date('Y-m-d',strtotime('-1 day'));
            $where = ['statistics_date' => $datetime];
        }

        $data_time = $this->db($table)->value($string);
        if ((string)$data_time === (string)$date){
            $this->db($table)->where($where)->delete(true);
        }

        return $this->db($table)->insertAll($data);

    }

    /**
     * 获取最大日期
     */
    public function monthInfo($table)
    {
        return $this->db($table)->value('max(to_char(create_at,\'YYYY-MM-DD\')) as create_at');
    }


    /**
     * 修改信息
     */
    public function monthEdit($table , $where ,  $date)
    {
        return $this->db($table)->where($where)->update($date);
    }

}
